﻿<%@ Page Language="C#" AutoEventWireup="true" Inherits="System.Web.UI.Page" %>
<%@ Import Namespace="Portal.Data.Entity" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Chọn thể loại truyện</title>
</head>
<body>
    <form id="form1" runat="server">
        <h1>
            <asp:Literal ID="bookName" runat="server"></asp:Literal></h1>
        <div runat="server" id="container" visible="False">
            <asp:ListBox ID="HistyoryIds" Rows="10" runat="server" DataValueField="Id" DataTextField="FullName" SelectionMode="Multiple" Width="100%"></asp:ListBox>

            <asp:HiddenField runat="server" ID="hidBid" />
            <asp:HiddenField runat="server" ID="hidSelected" />
            <br />
            Thể loại:
            <asp:Literal ID="ltrShow" runat="server"></asp:Literal><br />
            <br />
            <asp:Button ID="Save" runat="server" Text="Lưu lại" OnClick="Save_Click" />
            <input id="btnClose" type="button" value="Đóng lại" />
        </div>
        <div runat="server" id="noEntry" visible="True">
            <h1>Hãy chọn Book cụ thể!</h1>
        </div>
    </form>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script>
        jQuery(document).ready(function ($) {
            $("#btnClose").click(function () {
                var params = $('#<%=hidBid.ClientID%>').val() + "|" + $('#<%=hidSelected.ClientID%>').val();
                var radWindow = GetRadWindow();
                radWindow.argument = params;
                radWindow.close();
            });
        });
        function GetRadWindow() {
            var oWindow = null;
            if (window.radWindow) oWindow = window.radWindow;
            else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
            return oWindow;
        }
    </script>
</body>
</html>
<script runat="server">
    private int _bid;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Portal.Data.PortalContext.CurrentUser == null)
        {
            form1.Visible = false;
            return;
        }
        //binding
        if (!IsPostBack)
        {
            int.TryParse(Request["bid"], out _bid);
            if (_bid > 0)
            {
                var b = Book.FindByPrimaryKey(_bid);
                if (b != null)
                {
                    container.Visible = true;
                    noEntry.Visible = false;
                    hidBid.Value = b.ID.ToString();

                    HistyoryIds.DataSource = HistoryType.ListAll();
                    HistyoryIds.DataBind();

                    //binding
                    var selecteds = (b.HistoryTypeIds ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToInt32(x)).ToArray();
                    if (selecteds.Any())
                    {
                        foreach (int sel in selecteds)
                        {
                            HistyoryIds.Items.FindByValue(sel.ToString()).Selected = true;
                        }
                    }
                    bookName.Text = b.FullName;
                    BindShow(selecteds);
                }
            }
        }
    }

    private void Save_Click(object sender, EventArgs e)
    {
        var b = Book.FindByPrimaryKey(hidBid.Value);
        if (b != null)
        {
            var typeIds = HistyoryIds.Items.OfType<ListItem>().Where(item => item.Selected).Select(x => Convert.ToInt32(x.Value)).ToArray();

            BookType.ChangedType(b.ID, typeIds);    //execute by sql command

            //set current display
            b.HistoryTypeIds = string.Join(",", typeIds);
            BindShow(typeIds);
        }
    }

    void BindShow(int[] historyIds)
    {
        var results = HistoryType.ListAll().Where(x => historyIds.Contains(x.ID)).ToList();
        var s = results.Aggregate("", (current, ht) => current + string.Format("<a href='{0}' class='booktype'>{1}</a>, ", ht.Url, ht.FullName));
        s = string.IsNullOrEmpty(s) ? "" : s.Remove(s.Length - 2);

        hidSelected.Value = s;
        ltrShow.Text = s;
    }
</script>
